import georegression.struct.point.Point2D_I32;

import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.util.List;

import boofcv.alg.feature.detect.edge.CannyEdge;
import boofcv.alg.filter.binary.BinaryImageOps;
import boofcv.alg.filter.binary.Contour;
import boofcv.core.image.ConvertBufferedImage;
import boofcv.factory.feature.detect.edge.FactoryEdgeDetectors;
import boofcv.struct.image.ImageSInt16;
import boofcv.struct.image.ImageUInt8;

import com.github.sarxos.webcam.Webcam;


public class Canny extends Thread {
	Webcam webcam;
	GestureRecognition gr;

	public Canny(Webcam webcam, GestureRecognition gr) {
		this.webcam = webcam;
		this.gr = gr;
	}

	public void run(){
		while(!gr.isEnd()){
			//20~50ms 1000/50=20fps
			// 15fps~50fps
			long start = System.currentTimeMillis();
			BufferedImage image = webcam.getImage();

			ImageUInt8 gray = ConvertBufferedImage.convertFrom(image,(ImageUInt8)null);
			ImageUInt8 edgeImage = new ImageUInt8(gray.width,gray.height);

			// Create a canny edge detector which will dynamically compute the threshold based on maximum edge intensity
			// It has also been configured to save the trace as a graph.  This is the graph created while performing
			// hysteresis thresholding.
			CannyEdge<ImageUInt8,ImageSInt16> canny = FactoryEdgeDetectors.canny(2,true, true, ImageUInt8.class, ImageSInt16.class);

			// The edge image is actually an optional parameter.  If you don't need it just pass in null
			canny.process(gray,0.1f,0.3f,edgeImage);

			// First get the contour created by canny
			//List<EdgeContour> edgeContours = canny.getContours();
			// The 'edgeContours' is a tree graph that can be difficult to process.  An alternative is to extract
			// the contours from the binary image, which will produce a single loop for each connected cluster of pixels.
			// Note that you are only interested in external contours.
			List<Contour> contours = BinaryImageOps.contour(edgeImage, 8, null);

			// display the results
			//BufferedImage visualBinary = VisualizeBinaryData.renderBinary(edgeImage, null);
			/*BufferedImage visualCannyContour = VisualizeBinaryData.renderContours(edgeContours,null,
							gray.width,gray.height,null);*/
			//BufferedImage visualEdgeContour = VisualizeBinaryData.renderExternal(contours, null,
			//		gray.width, gray.height, null);

			/*ShowImages.showWindow(visualBinary,"Binary Edges from Canny");
					ShowImages.showWindow(visualCannyContour,"Canny Trace Graph");
					ShowImages.showWindow(visualEdgeContour,"Contour from Canny Binary");
			 */
			long cost = System.currentTimeMillis()-start;
			System.out.println("tim1:"+cost);
			start = System.currentTimeMillis();
			/*gr.getGraphics().clearRect(0, 0, gr.getWidth(), gr.getHeight());
			for(Contour contour : contours){
				List<Point2D_I32> points = contour.external;
				Polygon poly = new Polygon();
				for(Point2D_I32 p : points){
					poly.addPoint(p.x, p.y);
				}
				gr.getGraphics().drawPolygon(poly);
			}*/
			gr.getGraphics().drawImage(image, 0, 0, gr);
			/*for(Contour contour : contours){
						List<Point2D_I32> points = contour.external;
						Polygon poly = new Polygon();
						for(Point2D_I32 p : points){
							poly.addPoint(p.x, p.y);
						}
						getGraphics().drawPolygon(poly);
						break;
					}*/
			cost = System.currentTimeMillis()-start;
			System.out.println("tim2:"+cost);
			//getGraphics().drawImage(visualCannyContour, 0, 0, gray.width, gray.height, null);
		}
	}
}
